"""
Case Type   : 兼容M*数据库subdate函数
Case Name   : SUBDATE(TIME,INTERVAL)函数，参数1为time类型,time取值范围测试
              interval单位测试
Create At   : 2023.01.29
Owner       : z@wan005
Description :
    1、time覆盖左右边界
    2、超过左右边界
    3、非法时间
    4、清理环境
Expect      :
    1、成功
    2、报错
    3、报错
    4、成功
History     :modified by zhanghuan96，2024.06.28，优化断言
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest017(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_subdate_0017"

    def test_subdate(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----前置条件：创建兼容b库---')
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = '--step1:time覆盖左右边界;expect:成功--'
        self.log.info(text)
        sql_cmd = "SELECT SUBDATE(time'-838:59:59', -interval 2 hour);" \
                  "SELECT SUBDATE(time'838:59:59', interval 2 hour);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 8, '行数不到9行')
        self.assertEqual('-836:59:59',
                         sql_res.splitlines()[2].strip(), '执行失败' + text)
        self.assertEqual('836:59:59',
                         sql_res.splitlines()[-2].strip(), '执行失败' + text)

        text = '--step2:超过左右边界;expect:报错--'
        self.log.info(text)
        sql_cmd = "SELECT SUBDATE(time'-838:59:59.9', interval 2 hour);" \
                  "SELECT SUBDATE(time'839:59:59', interval 2 hour);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertEqual(2, sql_res.count(
            'ERROR:  truncated incorrect time value'), '执行失败' + text)

        text = '--step3:非法时间;expect:报错--'
        self.log.info(text)
        sql_cmd = "SELECT SUBDATE(time'83:25:60', interval 2 hour);" \
                  "SELECT SUBDATE(time'83:60:59', interval 2 hour);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertEqual(2, sql_res.count(
            'ERROR:  date/time field value out of range'), '执行失败' + text)

    def tearDown(self):
        text = '----step4:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname='postgres')
        self.log.info(drop_res1)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
